# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
# from itemadapter import HighVoltageItem
# from itemadapter import SensorOverrunItem
# import mysqlclient
import pymysql
import scrapy



class CcmkPipeline:
    conn = None
    cursor = None


    def open_spider(self, spider):
        self.conn = pymysql.Connect(host='127.0.0.1', port=3306, user='gpf', password='123456', db='zdhbb',
                                    charset='utf8')


    def process_item(self, item, spider):
        self.cursor = self.conn.cursor()

        try:
            self.cursor.execute('insert into zdhbb values("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")'%(item['FixedLocation'],item['MonitorCode'],item['AlarmDuration'],item['MinValues'],item['AvgValues'],item['MaxValue'],item['AlarmEndTime'],item['AlarmDate'],item['AlarmStartTime'],item['AlarmMark'],item['sensor_type'],item['sensor_name']))

            self.conn.commit()
        except Exception as e:
            print(e)
            self.conn.rollback()

        return item


    def close_spider(self, spider):
        self.cursor.close()
        self.conn.close()